package io.renren.modules.noc.service.impl;

import io.renren.modules.noc.dao.NocRoleDao;
import io.renren.modules.noc.entity.NocRoleEntity;
import io.renren.modules.noc.entity.NocTemplateEntity;
import io.renren.modules.noc.service.NocTemplateService;
import io.renren.modules.sys.dao.SysDeptDao;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.noc.dao.NocLyDao;
import io.renren.modules.noc.entity.NocLyEntity;
import io.renren.modules.noc.service.NocLyService;


@Service("nocLyService")
public class NocLyServiceImpl extends ServiceImpl<NocLyDao, NocLyEntity> implements NocLyService {

    @Autowired
    private NocTemplateService nocTemplateService;
    @Autowired
    private NocRoleDao roleDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private NocRoleDao nocRoleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        String tid = (String) params.get("tid");
        String deptId = (String) params.get("deptId");

        //获取系统用户的来源权限
        Map<String,Object> roleMap = new HashMap<>();
        roleMap.put("tid",tid);
        roleMap.put("uid",userEntity.getUserId());
        List<String> in = roleDao.getUserLyRole(roleMap);

        //根据部门取用户取来源
        if(StringUtils.isNotEmpty(deptId)){
            Map<String,Object> userMap = new HashMap<>();
            userMap.put("dept_id",deptId);
            List<SysUserEntity> userList = sysUserDao.selectByMap(userMap);
            List<Long> userStrList = new ArrayList<>();
            for(SysUserEntity user: userList) {
                userStrList.add(user.getUserId());
            }
            List<NocRoleEntity> roleList = nocRoleDao.selectList(
                    new QueryWrapper<NocRoleEntity>()
                    .in("user_id",userStrList)
            );
            for(NocRoleEntity nocRole : roleList){
                in.add(nocRole.getLy());
            }
        }

        IPage<NocLyEntity> page = this.page(
                new Query<NocLyEntity>().getPage(params),
                new QueryWrapper<NocLyEntity>()
                        .eq(tid != null, "tid", tid)
                        .in(in.size()>0,"ly",in)  //权限
        );

        for(NocLyEntity nocLyEntity : page.getRecords()){
            NocTemplateEntity nocTemplateEntity = nocTemplateService.getById(nocLyEntity.getTid());
            if(nocTemplateEntity != null){
                nocLyEntity.setTname(nocTemplateEntity.getTemplate());
            }
        }

        return new PageUtils(page);
    }

}
